package ywpt.service.config;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.Department;
import ywpt.web.common.PublicFunction;
@Service("departmentService")
@RemoteProxy(name="departmentService")
public class DepartmentServiceImpl extends HibernateEntityDao<Department> implements DepartmentService {
	public  DepartmentServiceImpl (){
		
	}

	@Override
	public Department getDepartmentById(Long id) {
		return this.get(id);
	}

	@Override
	public int getDepartmentCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Department> getDepartments(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public String creatDepartCode() {
		DetachedCriteria query = DetachedCriteria.forClass(Department.class, "depart");
		String departCode="";
		String code="";
		Integer maxId = new Integer(0);
		query.addOrder(Order.desc("id"));
		List<Department> departments = this.getDepartments(query);
		if(departments.iterator().hasNext()){
			code = departments.iterator().next().getDepartCode().substring(2);
			maxId = new Integer(code);
		}
		departCode = PublicFunction.getNextCode("",maxId,false,3);
		query.setProjection(null);
		return ("BM"+departCode);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Department> getDepartments(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@Override
	public void saveDepartment(Department department) {
		this.save(department);
	}

	@Override
	public void updateDepartment(Department department) {
		this.update(department);
	}

	@Override
	public void removeDepartment(Long id) {
		this.removeById(id);
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<Department> getDepartmentsByDepartName(String departName, int start,
			int size) {
		DetachedCriteria query = DetachedCriteria.forClass(Department.class, "department");
		//query.add(Restrictions.eq("organType", "01"));
		if ( departName != null && !departName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("departName", "%" + departName + "%"),  
							          Restrictions.like("departCode", "%" + departName + "%")));
				
		}
		
		query.addOrder(Order.asc("departCode"));
		List<Department> departmentList = new ArrayList<Department>();
		List<Department> departmentLists = new ArrayList<Department>();
		Department department =new Department();
		
		departmentList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<Department> itr = departmentList.iterator(); itr.hasNext(); i++ ) 
		{
			department = itr.next();
			String ss = PublicFunction.format(department.getDepartCode() + "",7, "  ", 1)
			          //+ PublicFunction.format(organ.getOrganName() + "", 14, "  ", 1);
			          + department.getDepartName(); 
			
			department.setDepartName(ss);
			departmentLists.add(department);
		}
		return departmentLists;
	}

	@Override
	@RemoteMethod
	public int getDepartmentsCountByName(String departName) {
		DetachedCriteria query = DetachedCriteria.forClass(Department.class, "department");
		//query.add(Restrictions.eq("organType", "01"));
		if ( departName != null && !departName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("departName", "%" + departName + "%" ),  
							          Restrictions.like("departCode", "%" + departName + "%" )));
				 //.add(Restrictions.eq("organType", "01"));
		}
		query.setProjection(null);
		return 	this.getCountByCriteria(query);
	}

	@Override
	public List<Department> getDepartInfos() {
		return this.getAll();
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<Department> getDepartmentsByIds(String ids) {
		if(ids.length()>0)
			ids=ids.substring(0,ids.length()-1);
		String idstr[]=ids.split(",");
		Long id[]=new Long[idstr.length];
		for(int i=0;i<idstr.length;i++)
			id[i]=Long.parseLong(idstr[i]);
		DetachedCriteria query=DetachedCriteria.forClass(Department.class);
		query.add(Restrictions.in("id",id));
		List<Department> list=this.findAllByCriteria(query);
		return list;
	}

}
